//报告生成函数声明
void generate_and_export_report(const char* name, int id, const char* classroom, 
                               float* daily_scores, float* final_scores, 
                               float total_score, int rank, 
                               const char* format, const char* filename);

sqlite3* db = NULL;
// ====================== 报告生成与导出实现 ======================
void generate_and_export_report(const char* name, int id, const char* classroom, 
                               float* daily_scores, float* final_scores, 
                               float total_score, int rank, 
                               const char* format, const char* filename) {
    FILE *fp = fopen(filename, "w");
    if (!fp) {
        printf("错误：无法打开文件 %s\n", filename);
        return;
    }

    // 计算每科加权分（平时40% + 期末60%）
    float course_score[5];
    for (int i = 0; i < 5; i++) {
        course_score[i] = daily_scores[i] * 0.4 + final_scores[i] * 0.6;
    }

    if (strcmp(format, "txt") == 0) { // TXT表格格式
        fprintf(fp, "==================== 成绩报告 ====================\n");
        fprintf(fp, "姓名：%s 学号：%d 班级：%s 总成绩：%.2f 班级排名：%d\n",name,id,classroom,total_score,rank);
        fprintf(fp, "--------------------------------------------------\n");
        fprintf(fp, "| 科目               | 平时分 | 期末分 | 加权分 |\n");
        fprintf(fp, "--------------------------------------------------\n");
        for (int i = 0; i < 5; i++) {
            fprintf(fp, "| %-20s | %6.2f | %6.2f | %6.2f |\n", 
                    COURSE_NAMES[i],daily_scores[i],final_scores[i],course_score[i]);
        }
        fprintf(fp, "==================================================\n");
    } 
    else {
        printf("错误：不支持的格式 %s\n", format);
        fclose(fp);
        return;
    }

    fclose(fp);
    printf("报告已成功导出至：%s\n", filename);
}